package com.gcph.zhuoba.main.bean;

import java.util.ArrayList;
import java.util.List;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;

import com.gcph.zhuoba.R;
import com.gcph.zhuoba.app.GcphApplication;
import com.gcph.zhuoba.main.common.Constant;
import com.gcph.zhuoba.util.L;
import com.gcph.zhuoba.util.PicLoadUtil;

/**
 * 制作教案的球桌
 * @author zhanghejie
 */
public class Desk {
	//球桌图片及位置
	public static List<Bitmap> bitmaps = new ArrayList<Bitmap>();
	public static List<float[]> bmpsPos=new ArrayList<float[]>();
	
	//球高亮图片\袋高亮图片
	private static Bitmap STATUS_BALL_SELECTED;
	private static Bitmap STATUS_BAG_CANIN;
	private static Bitmap STATUS_BALL_CANSELECT;
	
	private static Bitmap TOOLS_LEIQU;
	private static Bitmap TOOLS_HONGBAO;
	private static Bitmap TOOLS_ZOUWEI;
	private static Bitmap TOOLS_LIJUAN;
	private static Bitmap TOOLS_HEDAN;
	private static Bitmap TOOLS_ZAYAOBAO;
	
	private static float DESK_INTERNAL_WIDTH;
	private static float DESK_INTERNAL_HEIGHT;
	
	private static float BALL_CANSELECT_RADIUS;
	private static float BALL_SELECTED_RADIUS;
	private static float BAG_CANIN_RADIUS;
	
	//状态码0初始  1 已采集球型、2已选择类型     3已选中目标球   4已选中球袋    5已选中K球  6录像
	public static int STEP_FLAG_INIT=0; 
	public static int STEP_FLAG_COLLECTED=1;
	public static int STEP_FLAG_SELECTED_TYPE=2;
	public static int STEP_FLAG_SELECTED_BALL=3;
	public static int STEP_FLAG_SELECTED_BAG=4;
	public static int STEP_FLAG_SELECTED_KBALL=5;
	public static int STEP_FLAG_VEDIO=6;
	
	public static int STEP_FLAG=0;
	
	//教案类型
	public static Integer RESOURCE_TEACHING_TYPE;
	
	//球的图片及位置
	//不可进球列表
	public static List<Ball> notCanInballList = new ArrayList<Ball>();
	//可进球列表
	public static List<Ball> canInBallList = new ArrayList<Ball>();
	//目标球
	public static Ball selectedBall = null;
	//要K的球
	public static Ball selectedKBall = null;
	//选择的进球袋
	public static int selectedBag = -1;
	//2540mmx1270mm
	public static float[][] bagPositions={{0f,0f},{254f,-10f},{508f,0f},{0f,254f},{254f,254f+10},{508f,254f}};
	public static float[][] bagRealPositions=null;
	
	public static int[] canInBag = null;
	
	//记录当前正在移动操作的元素序号及元素位置
	public static Integer ELEMENT_CURRENT_INDEX=-1 ;
	
	//元素列表
	public static List<Element> elementList = new ArrayList<Element>();
	public static void drawSelf(Canvas canvas,Paint paint)
	{
		float ballOffset = Constant.imgBallDiameter/2*Constant.ballRatio;
		initBitmap(ballOffset);
		
		//画球桌
		for(int i=0;i<bitmaps.size();i++){
			canvas.drawBitmap(PicLoadUtil.scaleToFit(bitmaps.get(i),Constant.ratio), bmpsPos.get(i)[0], bmpsPos.get(i)[1], paint);
		}
		
		//==画元素====
		Element tempElement = null;
		for (int i = 0; i < elementList.size(); i++) {
			tempElement = elementList.get(i);
			
			if(tempElement.getElementType()==Constant.ELEMENT_TYPE_LEIQU)
			{
				float ratio = DESK_INTERNAL_WIDTH*50/1024/TOOLS_LEIQU.getWidth();
				float radius = DESK_INTERNAL_WIDTH*50/1024/2.0f;
				canvas.drawBitmap(PicLoadUtil.scaleToFit(TOOLS_LEIQU,ratio), tempElement.getX()-radius, tempElement.getY()-radius, paint);
			}
			if(tempElement.getElementType()==Constant.ELEMENT_TYPE_HONGBAO)
			{
				float ratio = DESK_INTERNAL_WIDTH*100/1024/TOOLS_HONGBAO.getWidth();
				float width = DESK_INTERNAL_WIDTH*100/1024/2.0f;
				float height = DESK_INTERNAL_HEIGHT*96/506/2.0f;
				canvas.drawBitmap(PicLoadUtil.scaleToFit(TOOLS_HONGBAO,ratio), tempElement.getX()-width, tempElement.getY()-height, paint);
			}
			if(tempElement.getElementType()==Constant.ELEMENT_TYPE_ZOUWEI)
			{
				
				float ratio = DESK_INTERNAL_WIDTH*120/1024/TOOLS_ZOUWEI.getWidth();
				float radius = DESK_INTERNAL_WIDTH*120/1024/2.0f;
				canvas.drawBitmap(PicLoadUtil.scaleToFit(TOOLS_ZOUWEI,ratio), tempElement.getX()-radius, tempElement.getY()-radius, paint);
			}
			
			if(tempElement.getElementType()==Constant.ELEMENT_TYPE_LIJUAN)
			{
				float ratio = DESK_INTERNAL_WIDTH*133/1024/TOOLS_LIJUAN.getWidth();
				float width = DESK_INTERNAL_WIDTH*133/1024/2.0f;
				float height = DESK_INTERNAL_HEIGHT*69/506/2.0f;
				canvas.drawBitmap(PicLoadUtil.scaleToFit(TOOLS_LIJUAN,ratio), tempElement.getX()-width, tempElement.getY()-height, paint);
			}
			
			if(tempElement.getElementType()==Constant.ELEMENT_TYPE_ZAYAOBAO)
			{
				
				float ratio = DESK_INTERNAL_WIDTH*52/1024/TOOLS_ZAYAOBAO.getWidth();
				float width = DESK_INTERNAL_WIDTH*52/1024/2.0f;
				float height = DESK_INTERNAL_HEIGHT*100/506/2.0f;
				canvas.drawBitmap(PicLoadUtil.scaleToFit(TOOLS_ZAYAOBAO,ratio), tempElement.getX()-width, tempElement.getY()-height, paint);
			}
			
			if(tempElement.getElementType()==Constant.ELEMENT_TYPE_HEDAN)
			{
				float ratio = DESK_INTERNAL_WIDTH*170/1024/TOOLS_HEDAN.getWidth();
				float width = DESK_INTERNAL_WIDTH*170/1024/2.0f;
				float height = DESK_INTERNAL_HEIGHT*66/506/2.0f;
				canvas.drawBitmap(PicLoadUtil.scaleToFit(TOOLS_HEDAN,ratio), tempElement.getX()-width, tempElement.getY()-height, paint);
			}
		}
		//画球
		Ball ball = null;
		
		//=============================已选中类型========================================================================
		if(STEP_FLAG_SELECTED_TYPE == STEP_FLAG)
		{
			//若是k球或走位，高亮可进球
			if(Constant.RESOURCE_SMALL_TYPE_KQIU.equals(RESOURCE_TEACHING_TYPE) || Constant.RESOURCE_SMALL_TYPE_ZOUWEI.equals(RESOURCE_TEACHING_TYPE))
			{
				for(int i=0;i<canInBallList.size();i++){
					ball = canInBallList.get(i);
					canvas.drawBitmap(PicLoadUtil.scaleToFit(STATUS_BALL_CANSELECT,BALL_CANSELECT_RADIUS),ball.getX()-ballOffset*1.5f, ball.getY()-ballOffset*1.5f, paint);
				}
			}
			else//防守或解球，高亮所有球,除了白球
			{
				for(int i=0;i<canInBallList.size();i++){
					ball = canInBallList.get(i);
					canvas.drawBitmap(PicLoadUtil.scaleToFit(STATUS_BALL_CANSELECT,BALL_CANSELECT_RADIUS),ball.getX()-ballOffset*1.5f, ball.getY()-ballOffset*1.5f, paint);
				}
				
				for(int i=0;i<notCanInballList.size();i++){
					ball = notCanInballList.get(i);
					if(ball.getNo()!=24)
					{
						canvas.drawBitmap(PicLoadUtil.scaleToFit(STATUS_BALL_CANSELECT,BALL_CANSELECT_RADIUS),ball.getX()-ballOffset*1.5f, ball.getY()-ballOffset*1.5f, paint);
					}
				}
			}

		}
		
		//=============================已选中球========================================================================
		if(STEP_FLAG_SELECTED_BALL == STEP_FLAG)
		{
			//画选中的球它的可进袋
			if(null != selectedBall)
			{
				paint.setColor(Color.RED);
				canvas.drawBitmap(PicLoadUtil.scaleToFit(STATUS_BALL_SELECTED,BALL_SELECTED_RADIUS),selectedBall.getX()-ballOffset*1.5f, selectedBall.getY()-ballOffset*1.5f, paint);
				
				//K球教案或走位教案高亮袋让其可选 
				if(Constant.RESOURCE_SMALL_TYPE_KQIU.equals(RESOURCE_TEACHING_TYPE) || Constant.RESOURCE_SMALL_TYPE_ZOUWEI.equals(RESOURCE_TEACHING_TYPE))
            	{
					int[] canInBag = selectedBall.getCanInBag();
					Desk.canInBag = canInBag;
					paint.setColor(Color.GREEN);
					float[] bagPosition = null;
					float x = 0;
					float y = 0;
					for (int i = 0; i < canInBag.length; i++) {
						bagPosition = bagPositions[canInBag[i]];
						x=bagPosition[0]*Constant.ratio+Constant.OFFSET_X;
						y=bagPosition[1]*Constant.ratio+Constant.OFFSET_Y;
						
						canvas.drawBitmap(PicLoadUtil.scaleToFit(STATUS_BAG_CANIN,BAG_CANIN_RADIUS),x-ballOffset*2f, y-ballOffset*2f, paint);
					}
            	}
			}
			
			//若是k球或走位，高亮其他可进球
			if(Constant.RESOURCE_SMALL_TYPE_KQIU.equals(RESOURCE_TEACHING_TYPE) || Constant.RESOURCE_SMALL_TYPE_ZOUWEI.equals(RESOURCE_TEACHING_TYPE))
			{
				for(int i=0;i<canInBallList.size();i++){
					ball = canInBallList.get(i);
					if(!ball.equals(selectedBall))
					{
						paint.setColor(Color.GREEN);
						//canvas.drawCircle(ball.getX(), ball.getY(), ballOffset*1.5f, paint);
						
						canvas.drawBitmap(PicLoadUtil.scaleToFit(STATUS_BALL_CANSELECT,BALL_CANSELECT_RADIUS),ball.getX()-ballOffset*1.5f, ball.getY()-ballOffset*1.5f, paint);
					}
				}
			}
		}
		//=============================选中球袋后开始========================================================================
		if(Desk.STEP_FLAG == Desk.STEP_FLAG_SELECTED_BAG)
		{
			//若已选择目标球且没选进球袋，则画选中的球底色
			if(null != selectedBall && -1 != selectedBag)
			{
				canvas.drawBitmap(PicLoadUtil.scaleToFit(STATUS_BALL_SELECTED,BALL_SELECTED_RADIUS),selectedBall.getX()-ballOffset*1.5f, selectedBall.getY()-ballOffset*1.5f, paint);
			}
			
			//如果制作的为K球教案，则画K球底色
			if(Constant.RESOURCE_SMALL_TYPE_KQIU.equals(Desk.RESOURCE_TEACHING_TYPE))
			{
				//若尚未选要K的球 ，高亮所有可K的球。
				if(null == selectedKBall)
				{
					for(int i=0;i<notCanInballList.size();i++){
						ball = notCanInballList.get(i);
						if(!ball.equals(selectedBall) && ball.getNo()!=24)
						{
							canvas.drawBitmap(PicLoadUtil.scaleToFit(STATUS_BALL_CANSELECT,BALL_CANSELECT_RADIUS),ball.getX()-ballOffset*1.5f, ball.getY()-ballOffset*1.5f, paint);
						}
					}
					
					for(int i=0;i<canInBallList.size();i++){
						ball = canInBallList.get(i);
						if(!ball.equals(selectedBall) && ball.getNo()!=24)
						{
							canvas.drawBitmap(PicLoadUtil.scaleToFit(STATUS_BALL_CANSELECT,BALL_CANSELECT_RADIUS),ball.getX()-ballOffset*1.5f, ball.getY()-ballOffset*1.5f, paint);
						}
					}
				}
			}
		}
		//=============================选中球袋后结束========================================================================
		
		//高亮要k的球
		if(null != selectedKBall)
		{
			canvas.drawBitmap(PicLoadUtil.scaleToFit(STATUS_BALL_SELECTED,BALL_SELECTED_RADIUS),selectedKBall.getX()-ballOffset*1.5f, selectedKBall.getY()-ballOffset*1.5f, paint);
		}
		
		//画进球线路
		if(null != selectedBall && -1 != selectedBag)
		{
			paint.setColor(Color.WHITE);
			canvas.drawLine(selectedBall.getX(), selectedBall.getY(), bagRealPositions[selectedBag][0], bagRealPositions[selectedBag][1], paint);
		}
		//画可进球
		for(int i=0;i<canInBallList.size();i++){
			ball = canInBallList.get(i);
			canvas.drawBitmap(PicLoadUtil.scaleToFit(ball.getBitmap(),Constant.ballRatio), ball.getX()-ballOffset, ball.getY()-ballOffset, paint);
		}
		
		//画不可进球
		for(int i=0;i<notCanInballList.size();i++){
			ball = notCanInballList.get(i);
			canvas.drawBitmap(PicLoadUtil.scaleToFit(ball.getBitmap(),Constant.ballRatio),ball.getX()-ballOffset, ball.getY()-ballOffset, paint);
		}
	}
	
	/**
	 * 初始化
	 * @param ballOffset
	 */
	private static void initBitmap(float ballOffset)
	{
		DESK_INTERNAL_WIDTH = (Constant.imgDeskW-2*Constant.imgDeskFrame)*Constant.ratio;
		DESK_INTERNAL_HEIGHT = (Constant.imgDeskH-2*Constant.imgDeskFrame)*Constant.ratio;
		//初始化图片
		if(null == STATUS_BALL_CANSELECT)
		{
			STATUS_BALL_CANSELECT = BitmapFactory.decodeResource(GcphApplication.getInstance().getResources(), R.drawable.status_ball_canselect); 
			BALL_CANSELECT_RADIUS = ballOffset*1.5f/(STATUS_BALL_CANSELECT.getWidth()/2f);
			L.d("BALL_CANSELECT_RADIUS="+BALL_CANSELECT_RADIUS);
		}
		
		if(null == STATUS_BALL_SELECTED)
		{
			STATUS_BALL_SELECTED = BitmapFactory.decodeResource(GcphApplication.getInstance().getResources(), R.drawable.status_ball_selected); 
			BALL_SELECTED_RADIUS = ballOffset*1.5f/(STATUS_BALL_SELECTED.getWidth()/2f);
			L.d("BALL_SELECTED_RADIUS="+BALL_SELECTED_RADIUS);
		}
		
		if(null == STATUS_BAG_CANIN)
		{
			STATUS_BAG_CANIN = BitmapFactory.decodeResource(GcphApplication.getInstance().getResources(), R.drawable.status_bag_canin); 
			BAG_CANIN_RADIUS = ballOffset*2f/(STATUS_BAG_CANIN.getWidth()/2f);
			L.d("BAG_CANIN_RADIUS="+BAG_CANIN_RADIUS);
		}
		
		if(null == TOOLS_LEIQU)
		{
			TOOLS_LEIQU = BitmapFactory.decodeResource(GcphApplication.getInstance().getResources(), R.drawable.tools_leiqu); 
		}
		
		if(null == TOOLS_HONGBAO)
		{
			TOOLS_HONGBAO = BitmapFactory.decodeResource(GcphApplication.getInstance().getResources(), R.drawable.tools_hongbao); 
		}
		
		if(null == TOOLS_ZOUWEI)
		{
			TOOLS_ZOUWEI = BitmapFactory.decodeResource(GcphApplication.getInstance().getResources(), R.drawable.tools_zouwei); 
		}
		
		if(null == TOOLS_LIJUAN)
		{
			TOOLS_LIJUAN = BitmapFactory.decodeResource(GcphApplication.getInstance().getResources(), R.drawable.tools_lijuan); 
		}
		
		if(null == TOOLS_ZAYAOBAO)
		{
			TOOLS_ZAYAOBAO = BitmapFactory.decodeResource(GcphApplication.getInstance().getResources(), R.drawable.tools_zayaobao); 
		}
		
		if(null == TOOLS_HEDAN)
		{
			TOOLS_HEDAN = BitmapFactory.decodeResource(GcphApplication.getInstance().getResources(), R.drawable.tools_hedan); 
		}
	}
	
	/**
	 * 清除球桌上的所有元素
	 */
	public static void clear()
	{
		//bitmaps.clear();
		//bmpsPos.clear();
		
		notCanInballList.clear();
		canInBallList.clear();
		
		elementList.clear();
		selectedBall = null;
		selectedKBall = null;
		selectedBag = -1;
		
		STEP_FLAG = 0;
	}
}